/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @package    Feather-CM
 * @subpackage Javascript
 * @copyright  Copyright (c) 2006, Julien PORTALIER
 * @link       http://portalier.com/feather
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

_gettext_add_translations("fr",
{
	'Expand textarea':            'Agrandir la zone de texte',
	'Shrink textarea':            'Réduire la zone de texte',
	'Emphasis (Ctrl+I)':          'Emphase (Ctrl+I)',
	'Bold (Ctrl+B)':              'Gras (Ctrl+B)',
	'Underline (Ctrl+U)':         'Souligner (Ctrl+U)',
	'Deleted (Ctrl+D)':           'Barré (Ctrl+D)',
	'Blockquote (Ctrl+Q)':        'Bloc de citation (Ctrl+Q)',
	'Insert link (Ctrl+L)':       'Insérer un lien (Ctrl+L)',
	'Display media (Ctrl+M)':     'Insérer un média (Ctrl+M)',
	'Enter the URL to point to:': "Entrez l'URL vers laquelle pointer :",
	'Enter link title:':          "Entrez le titre du lien :",
	'Enter image location URL:':  "Entrez l'URL de l'image :"
});

Toolbar.Syntax = Toolbar.Text.extend(
{
	initialize: function(textarea, syntax)
	{
		this.buttons = [
			['more',       _('Expand textarea')],
			['less',       _('Shrink textarea')],
			[],
			['italic',     _('Emphasis (Ctrl+I)'),  'i'],
			['bold',       _('Bold (Ctrl+B)'),      'b'],
			['underline',  _('Underline (Ctrl+U)'), 'u'],
			['del',        _('Deleted (Ctrl+D)'),   'd'],
			[],
	//		['highlight',  _('Highlight')],
	//		['color',      _('Font color')],
	//		['sup',        _('Exponent')],
	//		['q',          _('Inline quote')],
	//		['br',         _('Line break')],
	//		[],
			['quote',      _('Blockquote (Ctrl+Q)'),       'q'],
	//		['pre',        _('Preformated text (Ctrl+P)'), 'p'],
			[],
			['link',       _('Insert link (Ctrl+L)'),   'l'],
			['media',      _('Display media (Ctrl+M)'), 'm']
		];
		
		this.syntax = 0;
		if (syntax)
		{
			this.selectSyntax = $(syntax);
			this.selectSyntax.addEvent('change', this.setSyntax.bind(this));
			this.setSyntax();
		}
		
		this.parent(textarea);
	},
	
	setSyntax: function()
	{
		this.syntax = parseInt(this.selectSyntax.value);
	},
	
	addButton: function(tag, title)
	{
		this.parent(tag, title, new DOM('img', {
			src: '/img/toolbars/syntax/' + tag + '.png',
			alt: tag
		}));
	},
	
	// text selection
	
	encloseSelection: function(wiki, tag)
	{
		if (this.syntax === 0)
			this.parent(wiki);
		else
			this.parent('<' + tag + '>', '</' + tag + '>');
	},
	
	selectionStartLinesWith: function(str)
	{
		this.replaceInSelection(/\n/gi, str, '\n');
	},
	
	// callbacks
	
	cb_italic:    function() { this.encloseSelection('//', 'i');   },
	cb_bold:      function() { this.encloseSelection('**', 'b');   },
	cb_underline: function() { this.encloseSelection('__', 'u');   },
	cb_del:       function() { this.encloseSelection('~~', 'del'); },
	
	cb_quote: function()
	{
		if (this.syntax === 0)
			this.selectionStartLinesWith('> ');
		else
			this.encloseSelection(null, 'blockquote');
	},
	
	cb_pre: function()
	{
		if (this.syntax === 0)
			this.selectionStartLinesWith(' ');
		else
			this.encloseSelection(null, 'pre');
	},
	
	cb_link: function()
	{
		var url = prompt(_("Enter the URL to point to:"));
		if (url)
		{
			url = url.trim();
			var str = this.getSelection(), text;
			if (!str)
				str = prompt(_('Enter link title:'));
			this.replaceSelection((this.syntax === 0) ?
				'"' + str + '":' + url :
				'<a href="' + url + '">' + str + '</a>'
			);
		}
	},
	
	cb_media: function()
	{
		var text;
		var url = prompt(_('Enter image location URL:'));
		if (url)
		{
			url = url.trim();
			var str = this.getSelection();
			
			if (this.syntax !== 0)
				this.replaceSelection('<img src="' + url + '" title="' + str + '" alt="" />');
			else
			{
				if (str)
					str = '|' + str;
				this.replaceSelection('{{' + url + (str ? '|' + str : '') + '}}');
			}
		}
	},
	
	cb_more: function() { this.setHeight(+100); },
	cb_less: function() { this.setHeight(-100); },
	
	setHeight: function(offset)
	{
		var h = parseInt(this.textarea.getStyle('height'));
		this.textarea.setStyle('height', h + offset + 'px');
		this.textarea.focus();
	}
});